{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "python中时间日期格式化符号：\n",
    "\n",
    "%y 两位数的年份表示（00-99）\n",
    "\n",
    "%Y 四位数的年份表示（000-9999）\n",
    "\n",
    "%m 月份（01-12）\n",
    "\n",
    "%d 月内中的一天（0-31）\n",
    "\n",
    "%H 24小时制小时数（0-23）\n",
    "\n",
    "%I 12小时制小时数（01-12） \n",
    "\n",
    "%M 分钟数（00=59）\n",
    "\n",
    "%S 秒（00-59）\n",
    "\n",
    "%a 本地简化星期名称\n",
    "\n",
    "%A 本地完整星期名称\n",
    "\n",
    "%b 本地简化的月份名称\n",
    "\n",
    "%B 本地完整的月份名称\n",
    "\n",
    "%c 本地相应的日期表示和时间表示\n",
    "\n",
    "%j 年内的一天（001-366）\n",
    "\n",
    "%p 本地A.M.或P.M.的等价符\n",
    "%U 一年中的星期数（00-53）星期天为星期的开始\n",
    "\n",
    "%w 星期（0-6），星期天为星期的开始\n",
    "\n",
    "%W 一年中的星期数（00-53）星期一为星期的开始\n",
    "\n",
    "%x 本地相应的日期表示\n",
    "\n",
    "%X 本地相应的时间表示\n",
    "\n",
    "%Z 当前时区的名称\n",
    "\n",
    "%% %号本身 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import datetime\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'2019-09-21 22:33:35'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "%H Hour (24-hour clock) as a decimal number [00,23].\n",
    "\n",
    "%M Minute as a decimal number [00,59].\n",
    "\n",
    "%S Second as a decimal number [00,61].\n",
    "\n",
    "%f Microsecond as a decimal number [0,999999], zero-padded on the left.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 获取当前日期时间(毫秒）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2019-09-22 16:09:17.292127\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'2019-09:09:17.292127'"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = datetime.datetime.now()\n",
    "print(a)\n",
    "a.strftime('%Y-%m:%M:%S.%f')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'201922:53:25.704382'"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "datetime.datetime.now().strftime('%Y%H%M%S.%f')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "213155"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "datetime.datetime.now().microsecond"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 获取秒级时间戳与毫秒级时间戳、微秒级时间戳"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1569076899.6045651\n",
      "1569076899\n",
      "1569076899605\n",
      "1569076899604565\n"
     ]
    }
   ],
   "source": [
    "t = time.time()\n",
    "\n",
    "print(t)                        #原始时间数据\n",
    "print(int(t))                   #秒级时间戳\n",
    "print(int(round(t * 1000)))     #毫秒级时间戳\n",
    "print(int(round(t * 1000000)))  #微秒级时间戳"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 将日期转为秒级时间戳"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1514774430\n"
     ]
    }
   ],
   "source": [
    "dt = '2018-01-01 10:40:30'\n",
    "ts = int(time.mktime(time.strptime(dt, \"%Y-%m-%d %H:%M:%S\")))\n",
    "print (ts)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 将秒级时间戳转为日期"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2018-01-13 00:27:10\n"
     ]
    }
   ],
   "source": [
    "ts = 1515774430\n",
    "dt = time.strftime(\"%Y-%m-%d %H:%M:%S\", time.localtime(ts))\n",
    "print(dt)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 时间格式转成另一种时间格式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2019-08-02 01:00:00\n"
     ]
    }
   ],
   "source": [
    "dt = '08/02/2019 01:00'\n",
    "dt_new = datetime.datetime.strptime(dt, '%m/%d/%Y %H:%M').strftime('%Y-%m-%d %H:%M:%S')\n",
    "print(dt_new)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 转结构体时间struct_time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "time.struct_time(tm_year=2018, tm_mon=9, tm_mday=6, tm_hour=21, tm_min=54, tm_sec=46, tm_wday=3, tm_yday=249, tm_isdst=-1)\n",
      "time.struct_time(tm_year=2017, tm_mon=2, tm_mday=4, tm_hour=14, tm_min=7, tm_sec=56, tm_wday=5, tm_yday=35, tm_isdst=0)\n"
     ]
    }
   ],
   "source": [
    "ta_dt = time.strptime(\"2018-09-06 21:54:46\", '%Y-%m-%d %H:%M:%S')  #日期时间转结构体 \n",
    "ta_ms = time.localtime(1486188476) #时间戳转结构体，注意时间戳要求为int，来源 比特量化\n",
    "print(ta_dt)\n",
    "print(ta_ms)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
